package com.yl.chainStore.entity.sick

import com.baomidou.mybatisplus.annotation.IdType
import com.baomidou.mybatisplus.annotation.TableField
import com.baomidou.mybatisplus.annotation.TableId
import com.baomidou.mybatisplus.annotation.TableName
import com.yl.chainStore.common.annotations.CompanyUserOperationLogColumn
import com.yl.chainStore.common.enums.Status
import com.yl.chainStore.common.enums.TypeEnum
import com.yl.chainStore.entity.customer.CustomerInfo
import com.yl.chainStore.pojo.vo.customer.CustomerLeftJoinInfo
import com.yl.chainStore.util.*
import com.yl.common.annotation.NoArg
import com.yl.common.getContext
import java.util.*

/**
 * 就诊记录明细
 */
@NoArg
@TableName("sick_medical_records_detail")
data class SickMedicalRecordsDetail(

	/**
	 * id
	 */
	@TableId(value = "id", type = IdType.AUTO)
	var id: Long = 0,

	/**
	 * 开始时间
	 */
	@TableField("start_time")
	@CompanyUserOperationLogColumn(value = "就诊时间",columnType = TypeEnum.COMPANY_USER_OPERATION_LOG_COLUMN_DATE_TIME)
	var startTime: Date = Date(),

	/**
	 * 结束时间
	 */
	@TableField("end_time")
	var endTime: Date? = null,

	/**
	 * 日期
	 */
	@TableField("to_day")
	var toDay: Date = Date(),

	/**
	 * 咨询师
	 */
	@TableField("secondary_user_id")
	@CompanyUserOperationLogColumn(value = "咨询师",columnType = TypeEnum.COMPANY_USER_OPERATION_LOG_COLUMN_COMPANY_USER)
	var secondaryUserId: Long? = null,

	/**
	 * 咨询师名称
	 */
	@TableField(exist = false)
	var secondaryUserName: String? = null,

	/**
	 * 医生
	 */
	@TableField("master_user_id")
	@CompanyUserOperationLogColumn(value = "医生",columnType = TypeEnum.COMPANY_USER_OPERATION_LOG_COLUMN_COMPANY_USER)
	var masterUserId: Long ?= null,

	/**
	 * 医生名称
	 */
	@TableField(exist = false)
	var masterUserName: String? = null,

	/**
	 * 内容
	 */
	@TableField("content")
	@CompanyUserOperationLogColumn(value = "内容",columnType = TypeEnum.COMPANY_USER_OPERATION_LOG_COLUMN_TYPE_DEFAULT)
	var content: String? = null,

	/**
	 * 就诊记录id
	 */
	@TableField("record_id")
	var recordId: Long,

	/**
	 * 预约记录
	 */
	@TableField(exist = false)
	var record: SickMedicalRecords? = null,

	/**
	 * 患者id
	 */
	@TableField(exist = false)
	var customerId: Long?=null,

	/**
	 * 其他内容
	 */
	@TableField("other")
	var other: Any? = null,

	/**
	 * 所属那个分院
	 */
	@TableField("dept_id")
	var deptId: Long = getContext().subCompanyId,

	@TableField(exist = false)
	var subCompanyName: String? = null,

	/**
	 * 诊断类型
	 */
	@TableField("detail_type")
	@CompanyUserOperationLogColumn(value = "就诊类型",columnType = TypeEnum.COMPANY_USER_OPERATION_LOG_COLUMN_TYPE_ENUM)
	var detailType: TypeEnum = TypeEnum.SICK_MEDICAL_RECORD_DETAIL_TYPE_FIRST,

	/**
	 * 来源类型
	 */
	@TableField("medical_sources_type")
	@CompanyUserOperationLogColumn(value = "来源类型",columnType = TypeEnum.COMPANY_USER_OPERATION_LOG_COLUMN_TYPE_ENUM)
	var medicalSourcesType: TypeEnum ?= null,

	/**
	 * 来源
	 */
	@TableField("medical_sources")
	@CompanyUserOperationLogColumn(value = "就诊来源",columnType = TypeEnum.COMPANY_USER_OPERATION_LOG_COLUMN_TYPE_DEFAULT)
	var medicalSources: String? = null,


	/**
	 * 挂号 todo:可以不要
	 */
	@TableField("registered_num")
	var registeredNum: String = resNumCreate.generateOrder(getContext().subCompanyId.toString()),

	/**
	 * 状态  SUBSCRIBE             预约
	 *      NON_ARRIVAL           未到
	 *      WAIT_AUDIT            等待
	 *      IN_THE_TREATMENT     治疗中
	 *      COMPLETE_TREATMENT   治疗完成
	 *      LEAVE                离店
	 *      CANCEL              取消
	 */
	@TableField("status")
	@CompanyUserOperationLogColumn(value = "状态",columnType = TypeEnum.COMPANY_USER_OPERATION_LOG_COLUMN_STATUS_ENUM)
	var status: Status = Status.SUBSCRIBE,


	/**
	 * 备注
	 */
	@TableField("remark")
	@CompanyUserOperationLogColumn(value = "备注",columnType = TypeEnum.COMPANY_USER_OPERATION_LOG_COLUMN_TYPE_DEFAULT)
	var remark: String? = null,

	/**
	 * 预约id   没关联预约的为空
	 */
	@TableField("subscribe_log_id")
	var subscribeLogId: Long? = null,

	/**
	 * 病历id
	 */
	@TableField("history_id")
	var historyId:Long ?= null,

	@TableField(exist = false)
	var subscribeLog: CustomerSubscribeLog? = null
) :CustomerLeftJoinInfo(){
	companion object {
		private val resNumCreate: DeptOrderRegisteredNum = DeptOrderRegisteredNum()
	}


	val showTime: String
		get() {
			val str = timeFormat(startTime)
			return if (endTime != null) {
				str + "-" + timeFormat(endTime!!)
			} else {
				str
			}

		}

	val showYear: String
		get() {
			return yearFormat(toDay)
		}

	val showDay: String
		get() {
			return dayFormat(toDay)
		}

	val showStatus: String
		get() {
			return if (status == Status.NON_ARRIVAL ) {
				//未到的处理下
				if (startTime.before(Date())) {
					"迟到" + formatDuring(Date().time - startTime.time)
				} else {
					status.show
				}
			} else {
				status.show
			}
		}

	val canStatusUpdate: List<Map<String, Any>>
		get() {
			val resList = mutableListOf<Map<String, Any>>()
			resList.add(selectStatusMap("挂号", Status.WAIT_AUDIT))
			resList.add(selectStatusMap("开始治疗", Status.IN_THE_TREATMENT))
			resList.add(selectStatusMap("治疗结束", Status.COMPLETE_TREATMENT))
			resList.add(selectStatusMap("离店", Status.LEAVE))
			return resList
		}


	fun selectStatusMap(name: String, value: Status): Map<String, Any> {
		val map = mutableMapOf<String, Any>()
		map["name"] = name
		map["value"] = value.show
		map["canUse"] = status.code < value.code
		return map
	}


}
